<?php
	$base = array("rice","black beans");
        $build = array("bell pepper","onions");
	$cost = 21.99;
        $about = '<b>Donald\'s&nbsp;Rib-Eye</b>: 21 Oz. Steak';
        $extra = array("bacon","t bone");
        $meat = array("rib eye");

	//class that builds each plate
	class testMenu extends PHPUnit_Framework_TestCase {

		//menu attributes
		public $meat;
		public $meat_options; //other possibilities
		public $base; //rice beans etc.
		public $base_options;
		public $build; //ingredients 
		public $build_options;
		public $calories; //total calories
		public $options; //compile all ingredients into options
		public $cost; //subject to increase according to modifications
		public $description; //additional items

		//build the item according to the menu
		public function __construct() {
			global $build, $base, $meat, $about, $cost;
			//collect all arguments
			$args = array($build,$base,$meat,$about,$cost);

			//add build ingredients
			foreach($args[0] as $values) {
				$this->build[$values['name']] = $values; //manipulate name indice
				$this->calories += $values['calories']; //add calories
			}

			//base location. like rice, beans etc
			if(count($args) > 2) 
				foreach($args[1] as $values) { 
					$this->base[$values['name']] = $values; // add base ingredients
					$this->calories += $values['calories']; //add calories
				}

			//add meat 
			if(count($args) > 3) 
				foreach($args[2] as $values) {
					$this->meat[$values['name']] = $values;
					$this->calories += $values['calories'];
				}

			//item description
			if(count($args) > 4)
				$this->description = $args[3];

			//the cost of the plate
			$this->cost = $args[count($args)-1];

		} //conclude constructor

		//modify the plate
		public function testModify() {
			$exclude=array("onions");
			$replace=array();
			$add=array();
			$rows=array();
			//unset the unwanted ingredients
			foreach($exclude as $disregard) {
				$this->assertNotNull($disregard);

				//unwanted build ingredients - jalapenos
				if(isset($this->build[$disregard])) {
					$this->calories -= $this->build[$disregard]['calories'];
					$this->assertGreaterThanOrEqual(0, $this->calories);
					unset($this->build[$disregard]); 
				}

				//unwanted bases - beans, rice, etc
				elseif(isset($this->base[$disregard])) {
					$this->calories -= $this->base[$disregard]['calories'];
					$this->assertGreaterThanOrEqual(0, $this->calories);
					unset($this->base[$disregard]);
				}

				//unwanted meats
				elseif(isset($this->meat[$disregard])) {
					$this->calories -= $this->meat[$disregard]['calories'];
					$this->assertGreaterThanOrEqual(0, $this->calories);
					unset($this->meat[$disregard]); 
				}
			} // end unsetting ingredients

			//replace items
			foreach($replace as $rep) {

				//replace unwanted build ingredients and keep track of calories
				if(isset($this->build[$rep[0]['name']])) {
					$this->calories -= $this->build[$rep[0]['name']]['calories'];
					$this->assertGreaterThanOrEqual(0, $this->calories);
					unset($this->build[$rep[0]['name']]); 
					$this->build[$rep[1]['name']] = $rep[1];
					$this->assertEquals($this->build[$rep[1]['name']], $rep[1]);

					$this->calories += $this->build[$rep[1]['name']]['calories'];
					$this->assertGreaterThanOrEqual($this->build[$rep[1]['name']]['calories'], $this->calories);
				}

				//replace unwanted build ingredients and keep track of calories
				elseif(isset($this->base[$rep[0]['name']])) {
					$this->calories -= $this->base[$rep[0]['name']]['calories'];
					$this->assertGreaterThanOrEqual(0, $this->calories);

					unset($this->base[$rep[0]['name']]); 
					$this->base[$rep[1]['name']] = $rep[1];
					$this->assertEquals($this->base[$rep[1]['name']], $rep[1]);

					$this->calories += $this->base[$rep[1]['name']]['calories'];
					$this->assertGreaterThanOrEqual($this->base[$rep[1]['name']]['calories'], $this->calories);
				}

				//replace unwanted build ingredients and keep track of calories
				elseif(isset($this->meat[$rep[0]['name']])) {
					$this->calories -= $this->meat[$rep[0]['name']]['calories'];
					$this->assertGreaterThanOrEqual(0, $this->calories);

					unset($this->meat[$rep[0]['name']]); 
					$this->meat[$rep[1]['name']] = $rep[1];
					$this->assertEquals($this->meat[$rep[1]['name']], $rep[1]);

					$this->calories += $this->meat[$rep[1]['name']]['calories'];
					$this->assertGreaterThanOrEqual($this->meat[$rep[1]['name']]['calories'], $this->calories);
				}
			} //conclude replace items function

			//check if user wants to add items
			foreach($rows as $index => $value) 
				foreach($add as $name) 

					//examine which index the value's in
					if(isset($value[$name])) 
						switch($index) { //check category
							case 'build':
								$this->build[$name] = $rows[$index][$name];
								$this->assertEquals($this->build[$name], $rows[$index][$name]);

								$this->calories += $this->build[$name]['calories'];	
								$this->assertGreaterThanOrEqual($this->build[$name]['calories'], $this->calories);
								break;
							case 'base':
								$this->base[$name] = $rows[$index][$name];
								$this->assertEquals($this->base[$name], $rows[$index][$name]);

								$this->calories += $this->base[$name]['calories']; 	
								$this->assertGreaterThanOrEqual($this->base[$name]['calories'], $this->calories);
								break;
							case 'meat':
								$this->meat[$name] = $rows[$index][$name];
								$this->assertEquals($this->meat[$name], $rows[$index][$name]);

								$this->calories += $this->meat[$name]['calories']; 	
								$this->assertGreaterThanOrEqual($this->meat[$name]['calories'], $this->calories);
								break;
						} //conclude switch

				//echo "<pre>here:" . print_r($this->base,true) . "</pre>\n";
			
		} //end the plate modifier

		//add items for user to modify
		public function testPossible_items($instruction=array(),$rows=array()) {
			$item = array("bacon","t bone");
			foreach($item as $option) { //build option arrays
				$this->assertNotNull($option);

				//scan the build array and determine if viable option
				if(isset($this->build[$option])) 
					$this->build_options[$option] = $this->build[$option];

				if(isset($this->base[$option])) 
					$this->base_options[$option] = $this->base[$option];

				if(isset($this->meat[$option])) 
					$this->meat_options[$option] = $this->meat[$option];

			} //conclude option loop

			foreach($instruction as $value) { //build from instructions
				$this->assertNotNull($value);

				if(isset($rows['build'][$value['name']])) 
					$this->build_options[$value['name']] = $rows['build'][$value['name']];
					$this->assertEquals($this->build_options[$value['name']], $rows['build'][$value['name']]);

				if(isset($rows['base'][$value['name']])) 
					$this->base_options[$value['name']] = $rows['base'][$value['name']];
					$this->assertEquals($this->base_options[$value['name']], $rows['base'][$value['name']]);

				if(isset($rows['meat'][$value['name']])) 
					$this->meat_options[$value['name']] = $rows['meat'][$value['name']];
					$this->assertEquals($this->meat_options[$value['name']], $rows['meat'][$value['name']]);
			}//conclude instruction loop

		} //conclude possible items

		//remove possible items. accepts arrays
		public function testRemove_possible() {
			$remove = array("onions");

			//loop through each item
			foreach($remove as $item) {
				$this->assertNotNull($item);
				if(isset($this->build_options[$item])) {
					$this->assertNotNull($this->build_options[$item]);
					unset($this->build_options[$item]);
				}
				if(isset($this->base_options[$item])) {
					$this->assertNotNull($this->base_options[$item]);
					unset($this->base_options[$item]);
				}
				if(isset($this->meat_options[$item])) {
					$this->assertNotNull($this->meat_options[$item]);
					unset($this->meat_options[$item]);
				}
			} //conclude loop
		} //conclude remove function

		//return calories,description, and cost
		public function testGet_calories() {
			$this->assertNotNull($this->calories);
			return $this->calories;
		}
		public function testGet_description() {
			$this->assertNotNull($this->description);
			return $this->description;
		}
		public function testGet_cost() {
			$this->assertNotNull($this->cost);
			return $this->cost;
		} //conclude calories and cost

		//user based output in checkbox form
		public function testUser_output_with() {
			$tmp = "";
			//begin looping through build ingredients
			foreach($this->build as $index => $value) {
				$this->assertNotNull($index);
				$tmp .= "<input type=\"checkbox\" name=\"without"
					. "{$value['name']}\" value=\"{$value['name']}\" />"
					. "{$value['name']}\n";
				$this->assertStringEndsWith("<input type=\"checkbox\" name=\"without"
					. "{$value['name']}\" value=\"{$value['name']}\" />"
					. "{$value['name']}\n", $tmp);
			}

			//begin looping through base ingredients
			foreach($this->base as $index => $value) {
				$this->assertNotNull($index);
				$tmp .= "<input type=\"checkbox\" name=\"without"
					. "{$value['name']}\" value=\"{$value['name']}\"" 
					. "/>{$value['name']}\n";
				$this->assertStringEndsWith("<input type=\"checkbox\" name=\"without"
					. "{$value['name']}\" value=\"{$value['name']}\"" 
					. "/>{$value['name']}\n", $tmp);
			}

			//begin looping through meat ingredients
			foreach($this->meat as $index => $value) {
				$this->assertNotNull($index);
				$tmp .= "<input type=\"checkbox\" name=\"without"
					. "{$value['name']}\" value=\"{$value['name']}\"" 
					. "/>{$value['name']}\n";
				$this->assertStringEndsWith("<input type=\"checkbox\" name=\"without"
					. "{$value['name']}\" value=\"{$value['name']}\"" 
					. "/>{$value['name']}\n", $tmp);
			}

			$this->assertNotNull($tmp);
			return $tmp; //return checkboxes

		} //conclude user based output 

		//user based output in checkbox form
		public function testPossible_output() {
			$tmp = "";
			//begin looping through build ingredients
			foreach($this->build as $index => $value) {
				$this->assertNotNull($index);
				$tmp .= "<input type=\"checkbox\" name=\"contain"
					. "{$value['name']}\" value=\"{$value['name']}\" />"
					. "{$value['name']}\n";
				$this->assertStringEndsWith("<input type=\"checkbox\" name=\"contain"
					. "{$value['name']}\" value=\"{$value['name']}\" />"
					. "{$value['name']}\n", $tmp);
			}

			//begin looping through base ingredients
			foreach($this->base as $index => $value) {
				$this->assertNotNull($index);
				$tmp .= "<input type=\"checkbox\" name=\"contain"
					. "{$value['name']}\" value=\"{$value['name']}\"" 
					. "/>{$value['name']}\n";
				$this->assertStringEndsWith("<input type=\"checkbox\" name=\"contain"
					. "{$value['name']}\" value=\"{$value['name']}\"" 
					. "/>{$value['name']}\n", $tmp);
			}

			//begin looping through meat ingredients
			foreach($this->meat as $index => $value) {
				$this->assertNotNull($index);
				$tmp .= "<input type=\"checkbox\" name=\"contain"
					. "{$value['name']}\" value=\"{$value['name']}\"" 
					. "/>{$value['name']}\n";
				$this->assertStringEndsWith("<input type=\"checkbox\" name=\"contain"
					. "{$value['name']}\" value=\"{$value['name']}\"" 
					. "/>{$value['name']}\n", $tmp);
			}

			$this->assertNotNull($tmp);
			return $tmp; //return checkboxes
		} //conclude user option check box



		//output the instructions of how to build plate
		public function output() {
			echo "<pre>" . print_r($this->build,true) . "</pre>\n";
			echo "<pre>" . print_r($this->base,true) . "</pre>\n";
			echo "<pre>" . print_r($this->meat,true) . "</pre>\n";
		} //end the output function

	} //conclude class

?>
